<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="文件名称" prop="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="bindFilesList">
      <el-table-column type="selection" width="55" align="center" />
      <!-- 添加序号列 -->
      <el-table-column
        label="序号"
        type="index"
        width="70"
        align="center"
        index-method="indexMethod"
      />

      <el-table-column
        label="文件名称"
        align="center"
        prop="fileName"
        width="300"
      >
        <template slot-scope="scope">
          <span>{{ getFileName(scope.row.fileName) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文件大小" align="center" prop="fileSize">
        <template slot-scope="scope">
          <span>{{ getFileSize(scope.row.fileSize) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="上传时间"
        align="center"
        prop="uploadTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.uploadTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column label="文件类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag
            class="dict-box"
            :options="dict.type.pur_notice_type"
            :value="scope.row.type"
          />
        </template>
      </el-table-column>

      <el-table-column label="创建人" align="center" prop="creater" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['purs:bindFiles:edit']"
            @click="loadFile(scope.row)"
            >下载</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改招标文件对话框 -->
    <el-dialog width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="文件名称" prop="fileName">
          <el-input v-model="form.fileName" placeholder="请输入文件名称" />
        </el-form-item>
        <el-form-item label="文件路径" prop="filePath">
          <el-input v-model="form.filePath" placeholder="请输入文件路径" />
        </el-form-item>
        <el-form-item label="文件大小" prop="fileSize">
          <el-input v-model="form.fileSize" placeholder="请输入文件大小" />
        </el-form-item>
        <el-form-item label="上传时间" prop="uploadTime">
          <el-date-picker
            clearable
            v-model="form.uploadTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上传时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="creater">
          <el-input v-model="form.creater" placeholder="请输入创建人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listBindFiles,
  getBindFiles,
  delBindFiles,
  addBindFiles,
  updateBindFiles,
} from "@/api/purs/bindFiles";

export default {
  name: "BindFiles",
  props: {
    openMessage: {
      type: Object,
    },
  },
  dicts: ["pur_notice_type"],

  data() {
    return {
      // 遮罩层
      loading: true,

      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 招标文件表格数据
      bindFilesList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileName: null,
        filePath: null,
        fileSize: null,
        uploadTime: null,
        creater: null,
      },
      bindObj: {},
      // 表单参数
      form: {},
    };
  },
  created() {
    this.bindObj = this.openMessage;
    this.getList();
  },
  mounted() {},
  methods: {
    loadFile(row) {
      window.location.href = row.filePath;
    },
    indexMethod(index) {
      // 根据需要自定义序号生成方式，这里只是简单返回索引+1
      return index + 1;
    },
    getFileName(fileName) {
      const index = fileName.indexOf("-"); // 找到第一个减号的位置
      const result = fileName.slice(index + 1); // 获取从减号后一位开始到末尾的部分
      return result;
    },
    getFileSize(fileSize) {
      let size = parseFloat(fileSize).toFixed(1);
      return size + " Kb";
    },
    /** 查询招标文件列表 */
    getList() {
      this.loading = true;
      this.queryParams.bindCode = this.bindObj.bindCode;
      listBindFiles(this.queryParams).then((response) => {
        this.bindFilesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fileId: null,
        fileName: null,
        filePath: null,
        fileSize: null,
        uploadTime: null,
        creater: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>